<!--
    工单规则管理
 -->
<template>
    <div class="page">
        <div class="nuclein-query">
            <div>工单规则管理</div>
            <div style="margin-left: auto;">
                <el-button size="mini" type="primary" @click="handlerAdd()">添加</el-button>
            </div>
        </div>
        <!-- 表格 -->
        <div class="content-row">
            <el-table :data="tableData" style="width: 100%" border>
                <el-table-column label="工单规则id" prop="repairRuleId">
                </el-table-column>
                <el-table-column label="工单规则名称" prop="rulesName">
                </el-table-column>
                <el-table-column label="规定超出的时间 " prop="isOut">
                    <template slot-scope="scope">
                        {{ scope.row.isOut }}小时
                    </template>
                </el-table-column>
                <el-table-column label="是否开启工单超时创建" prop="isTimeoutCreate">
                    <template slot-scope="scope">
                        <el-tag :type="scope.row.isTimeoutCreate ? 'primary' : 'danger'">
                            {{ scope.row.isTimeoutCreate ? '开启' : '关闭' }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="160">
                    <template slot-scope="scope">
                        <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger"
                            @click="handleDelete(scope.row.repairRuleId)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <!-- 分页 -->
        <div class="pagination">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
                :page-sizes="[10, 20, 30, 40]" :page-size="size" layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>

        <!-- 弹出框 -->
        <div>
            <el-dialog :title="title" :visible.sync="dialogFormVisible">
                <el-form :model="form">
                    <el-form-item label="工单规则名称" :label-width="formLabelWidth">
                        <el-input v-model="form.rulesName" placeholder="请输入内容"></el-input>
                    </el-form-item>
                    <el-form-item label="设备型号id" :label-width="formLabelWidth">
                        <el-select v-model="form.equipmentType" placeholder="请选择设备id">
                            <el-option v-for="item in deviceIdList" :key="item.deviceId" :label="item.deviceName"
                                :value="item.deviceId">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="是否开启工单超时创建" :label-width="formLabelWidth">
                        <el-select v-model="form.isTimeoutCreate" placeholder="请选择设备id">
                            <el-option label="开启" value=1></el-option>
                            <el-option label="关闭" value=0></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="规定超出的时间" :label-width="formLabelWidth">
                        <el-input v-model="form.isOut" placeholder="请输入内容">
                            <template slot="append">小时</template>
                        </el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="Submite">确 定</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script>
import { repairRulesList, repairRulesAddEdit, repairRulesDelete, getEquipment, repairRulesEdit } from '../../api/api'
import commonFunction from '../../utils/commonFunction'
import commonData from '../../utils/commonData'
export default {
  data () {
    return {
      title: '添加',
      dialogFormVisible: false,
      deviceLogType: commonData.deviceLogType,
      total: 10,
      page: 1,
      size: 10,
      formLabelWidth: '160px',
      tableData: [],
      deviceIdList: [],
      form: {
        isOut: '',
        isTimeoutCreate: '',
        rulesName: '',
        equipmentType: ''
      }
    }
  },
  created () {
    
    this.getgetEquipment()
    setTimeout(() => {
      this.gitList()
    }, 1);
  },
  methods: {
    // 获取
    gitList () {
      const data = {
        page: this.page,
        limit: this.size
      }
      repairRulesList({ ...data }).then(res => {
        if (res.code == 0) {
          const { total, records } = res.page
          this.total = total
          this.tableData = records.map(item => {
            const deviceUpdateTime = commonFunction.getTimeFormat(+new Date(item.deviceUpdateTime), 'YYYY-MM-DD hh:mm:ss')
            return {
              ...item,
              deviceUpdateTime
            }
          })
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    getgetEquipment () {
      getEquipment().then(res => {
        if (res.code === 0) {
          this.deviceIdList = res.list
        }
      })
    },
    // 添加
    handlerAdd () {
      this.title = '添加'
      this.form = {
        isOut: '',
        isTimeoutCreate: '',
        rulesName: '',
        equipmentType: ''
      }
      this.dialogFormVisible = true
    },
    // 编辑
    handleEdit (row) {
      this.title = '编辑'
      this.dialogFormVisible = true
      this.form = {
        repairRuleId: row.repairRuleId,
        isOut: row.isOut,
        isTimeoutCreate: row.isTimeoutCreate + '',
        rulesName: row.rulesName,
        equipmentType: row.equipmentType + ''
      }
    },
    // 删除
    handleDelete (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        repairRulesDelete(row).then(res => {
          if (res.code == 0) {
            this.$message.success('删除成功')
            this.gitList()
          } else {
            this.$message.error(res.msg)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 确认提交 添加、编辑
    Submite () {
      const data = {
        repairRuleId: this.form.repairRuleId ? this.form.repairRuleId : null,
        ...this.form
      }
      if (data.repairRuleId) {
        repairRulesEdit(data).then(res => {
          if (res.code == 0) {
            this.$message.success('编辑成功')
            this.gitList()
            this.dialogFormVisible = false
          } else {
            this.$message.error(res.msg)
          }
        })
      } else {
        repairRulesAddEdit(data).then(res => {
          if (res.code == 0) {
            this.$message.success('添加成功')
            this.gitList()
            this.dialogFormVisible = false
          } else {
            this.$message.error(res.msg)
          }
        })
      }
    },
    // 分页
    handleSizeChange (val) {
      this.size = val
      this.gitList()
    },
    handleCurrentChange (val) {
      this.page = val
      this.gitList()
    }
  }
}
</script>

<style scoped>
.page {
    position: relative;
    height: 100%;
}

.nuclein-query {
    display: flex;
    align-items: center;
    padding: 20px;
    border-bottom: 1px solid #DCDFE6;
}

.content-row {
    overflow: hidden;
}

.device-type {
    padding: 20px 20px 10px 20px;
}

/* 分页 */
.pagination {
    width: 100%;
    padding: 20px;
    border-top: 1px solid #DCDFE6;
}
</style>
